package cn.infinite.security.configuration;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

import javax.annotation.Resource;

@Slf4j
@Configuration
@Order(97)
@ConditionalOnBean(ImageCheckCodeConfigProperties.class)
public class ImageCheckCodeAutoConfiguration extends WebSecurityConfigurerAdapter {

    public static final String CHILD_PATH_MATCHER = "/**";

    @Resource
    private ImageCheckCodeConfigProperties imageCheckCodeConfigProperties;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.antMatcher(imageCheckCodeConfigProperties.getScorePath().concat(CHILD_PATH_MATCHER)).authorizeRequests()
                .antMatchers(
                        this.imageCheckCodeConfigProperties.getImageCheckCodeGenerateEndPoint(),//获取图片验证码无需认证
                        this.imageCheckCodeConfigProperties.getJudgeImageCheckCodeEndPoint()//判断是否需要图片验证码认证
                ).permitAll().and().authorizeRequests().anyRequest().authenticated().and().csrf().disable();
    }

}
